package com.fzfnn.scrapbook.mapper;

import com.fzfnn.scrapbook.entity.Comment;
import com.fzfnn.scrapbook.vo.CommentVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    // 添加评论
    @Insert("INSERT INTO comment (journal_id, user_id, content, parent_id) " +
            "VALUES (#{journalId}, #{userId}, #{content}, #{parentId})")
    int addComment(Comment comment);

    // 查询某个手帐的评论（可加分页）
    @Select("SELECT * FROM comment WHERE journal_id = #{journalId} AND status = 1 ORDER BY created_at ASC")
    List<Comment> getCommentsByJournalId(@Param("journalId") Long journalId);

    // 删除评论（逻辑删除）
    @Update("UPDATE comment SET status = 0 WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int deleteComment(@Param("commentId") Long commentId, @Param("userId") Long userId);

    @Select("SELECT " +
            "c.comment_id AS commentId, " +
            "c.journal_id AS journalId, " +
            "c.user_id AS userId, " +
            "c.content, " +
            "c.created_at AS createdAt, " +
            "c.parent_id AS parentId, " +
            "c.status, " +
            "u.username AS commentName, " +
            "u.imgurl AS commentAvatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.user_id " +
            "WHERE c.journal_id = #{journalId} AND c.status = 1 " +
            "ORDER BY c.created_at ASC")
    List<CommentVO> selectCommentsByJournalId(@Param("journalId") Long journalId);



}
